'use strict';

/**
 * Module dependencies.
 */
var _ = require('lodash'),
    fs = require('fs'),
    path = require('path'),
    errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
    mongoose = require('mongoose'),
    User = mongoose.model('User');

/**
 * Send User
 */
exports.me = function(req, res) {
    User
    .findOne({
        Code: req.user.Code
    },  { 
        Code: 1, Name: 1, UniqueID: 1, Image: 1, Email: 1, Roles: 1, Phone: 1, Department: 1, Remark: 1,
        ErpEntCode: 1, ErpUserCode: 1, Orgs: 1
    })
    .populate([{ path: 'Department', select: 'Code Name Remark'},{path : 'Orgs', select : 'Code Name'}])
    .exec(function (err, user) {
        if (err) {
            res.json({
                status: 'EEEE',
                desc: errorHandler.getErrorMessage(err)
            });
        } else {
            if (!user) {
                res.json({
                    status: 'EEEE',
                    desc: '不存在该用户'
                });
            } else {
                res.json({
                    status: '0000',
                    data: user
                });
            }
        }
    });
};